//
//  SetNewPasswodController.m
//  coba-iOS
//
//  Created by 郭四虎 on 15/12/14.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "SetNewPasswodController.h"

@interface SetNewPasswodController ()
@property (weak, nonatomic) IBOutlet UILabel *numLb;
@property (weak, nonatomic) IBOutlet UITextField *pwdTf;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwd;

@end

@implementation SetNewPasswodController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (self.number) {
        _numLb.text = self.number;
    }
}
- (IBAction)submitClick:(id)sender {
    
    if ([self validPwd:_pwdTf.text]&&[self validPwd:_confirmPwd.text]) {
        
        if (![_confirmPwd.text isEqualToString:_pwdTf.text]) {
            [self.view Message:@"两次新密码输入不一样" HiddenAfterDelay:1];
            return;
        }
        
        NSDictionary *parameter = @{@"login_id":_numLb.text,@"new_password":[MyMD5 md5: _pwdTf.text]};
        
        [MyAFN POST:FORGETPWD parameters:parameter block:^(id responseObject) {
            
            NSDictionary *dic = responseObject;
            
            if ([[dic objectForKey:@"status"]integerValue]) {
                
                [[NSUserDefaults standardUserDefaults]setObject:_numLb.text forKey:@"userid"];
                
                [[NSUserDefaults standardUserDefaults]setObject:[MyMD5 md5: _pwdTf.text] forKey:@"userpwd"];
                
                [self toLogin];
                
                
            }
            
        }error:^(NSError *error) {
            
        } fromController:self];
        
    }else{
        
        [self.view Message:@"请输入6-32位密码" HiddenAfterDelay:1];
    }
    
        
}

- (void)message
{
    [self.view Message:@"修改密码成功，即将进入应用" HiddenAfterDelay:1];
}

- (void)getIn
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (BOOL)validPwd:(NSString *)pwd
{
    NSString *str = @"^\\d{6,23}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
    BOOL ismatch = [pred evaluateWithObject:pwd];
    
    return ismatch;
}

- (IBAction)showPasswod:(UISwitch *)sender {
    
    if (sender.on) {
        _pwdTf.secureTextEntry = NO;
        _confirmPwd.secureTextEntry = NO;
    }else{
        _pwdTf.secureTextEntry = YES;
        _confirmPwd.secureTextEntry = YES;
    }
}

- (void)toLogin
{
    if (USERID) {
        NSDictionary *parameters = @{@"login_id":USERID,@"password":USERPWD,@"login_ip":[Myuser getIPAddress],@"login_date":[MyDateformater getCurrentDate]};
        [MyAFN POST:LOGIN parameters:parameters block:^(id responseObject) {
            
            NSDictionary *dic = responseObject;
            
            if (dic) {
                if ([[dic objectForKey:@"status"]integerValue]) {
                    
                    [[Myuser sharedUser]setValuesForKeysWithDictionary:dic];
                    
                    [[NSUserDefaults standardUserDefaults]setObject:USERID forKey:@"userid"];
//                    [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"name"] forKey:@"username"];
//                    
//                    NSString *url = [NSString stringWithFormat:@"%@?url=%@",GETICON,[dic objectForKey:@"photo"]];
//                    [[NSUserDefaults standardUserDefaults]setObject:url forKey:@"usericon"];
                    
                    [[NSUserDefaults standardUserDefaults]setObject:USERPWD forKey:@"userpwd"];
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeUserinfo" object:nil];
                    
                    [self performSelector:@selector(message) withObject:nil afterDelay:0.5];
                    
                    [self performSelector:@selector(getIn) withObject:nil afterDelay:1.5];
                    
                }else{
                    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userid"];
                    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"usericon"];
                    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"username"];
                    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userpwd"];
                    
                    [self performSelector:@selector(showMsg:) withObject:dic afterDelay:0.5];
                }
                
            }
        }error:^(NSError *error) {
            
        }fromController:self];
    }
}

- (void)showMsg:(NSDictionary *)dic
{
    [self.view Message:[dic objectForKey:@"msg"] HiddenAfterDelay:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
